Marco d'Itri: ssh-agent for unattended processes
This code allows unattended ssh(1) processes, like the ones started by cron(8), to access the SSH keys stored by a running ssh-agent(1) (which will probably have been started in a screen(1) instance).
Be sure to understand the security implications of persistent ssh-agent processes and passwordless keys.
#!/bin/sh -e import_ssh_agent() local pid for pid in $(pgrep -u "$LOGNAME" -x -f /bin/bash); do local var="$(grep -z '^SSH_AUTH_SOCK=' /proc/$pid/environ true)" [ "$var" ] continue eval "$var" export SSH_AUTH_SOCK break done [ "$SSH_AUTH_SOCK" ] import_ssh_agent # now the script can run commands like: ssh -o BatchMode=yes hostname command